<template>
  <div class="my-sales">
    <!-- <navbar :title="'我的销售'"></navbar> -->
    <div class="section-1">
      <sales-amount-item :salesDetail="mySalesDetail" @onConfirm="onConfirm"></sales-amount-item>
    </div>

    <div class="section-2">
      <total-sales-amount :number="mySalesDetail.saleMoney"></total-sales-amount>
      <div class="product-list">
        <list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="getSaleVolumeItem"
          ref="loadList"
        >
          <div
            @click="goToSalesAmount(item)"
            class="item"
            v-for="(item, index) in saleVolumeList"
            :key="index"
          >
            <product-item :type="'click'" :cover="staticHost+item.goodsImg">
              <div class="products-item" slot="product-item">
                <p>
                  <span>{{item.goodsName}}</span>
                  <span class="size">{{item.size}}</span>
                </p>
                <p>
                  <span>销售额：</span>
                  <span class="price">￥{{item.saleMoney || '0.00'}}</span>
                </p>
                <p>
                  <span>销售折扣：</span>
                  <span class="price">￥{{item.saleDiscount || '0.00'}}</span>
                </p>
              </div>
            </product-item>
          </div>
        </list>
      </div>
    </div>
  </div>
</template>

<script>
import productItem from "../components/product_item.vue";
import salesAmountItem from "../components/sales_amount_item.vue";
import totalSalesAmount from "../components/total_sales_amount.vue";
import navbar from "../components/navbar";
import http from "../service/myHttp.js";
import urls from "../service/url.js";
import { mapState } from "vuex";
let moment = require("moment");
import { staticHost } from "../service/HOST";
import { List } from "vant";

export default {
  components: {
    productItem,
    salesAmountItem,
    totalSalesAmount,
    navbar,
    List
  },
  data() {
    return {
      staticHost,
      loading: false,
      finished: false,
      pageNumber: 1,
      pageSize: 10,
      date: "",
      mySalesDetail: {},
      saleVolumeList: []
    };
  },
  computed: {
    ...mapState({
      userInfo: state => state.login_module.userinfo,
      countAlias: "userInfo"
    })
  },
  mounted() {
    this.date = localStorage.getItem("mysales_startDate")
      ? localStorage.getItem("mysales_startDate")
      : moment(new Date()).format("YYYY-MM");

    this.getMySalesDetail();
    // this.getSaleVolumeItem();
  },
  methods: {
    getSaleVolumeItem() {
      return this.$http
        .post(this.$urls.HQXSMX, {
          startDate: this.date,
          pageNumber: this.pageNumber,
          pageSize: this.pageSize
        })
        .then(res => {
          if (res.data.code == 200) {
            this.saleVolumeList = this.saleVolumeList.concat(res.data.data.rows);

            this.pageNumber++;
            this.loading = false;
          } else if (res.data.code == 300) {
            this.loading = false;
            this.finished = true;
          }
        });
    },
    // 获取我的销售数据
    getMySalesDetail() {
      return http
        .post(urls.WDXS, {
          agentLevel: this.userInfo.agentLevel,
          userId: this.userInfo.id,
          // tokenId: this.userInfo.id,
          startDate: this.date
        })
        .then(res => {
          if (res.data.code == 200) {
            // if(this.date.indexOf('2020') != -1){
            //   res.data.data.curEarn = 10
            //   res.data.data.saleMoney = 103
            // }
            this.mySalesDetail = res.data.data;
          }
        })
        .catch(err => {
          console.log("错误：", err);
        });
    },
    onConfirm(date) {
      console.log(date);
      this.date = date;
      localStorage.setItem("mysales_startDate", this.date);

      this.loading = false;
      this.finished = false;
      this.pageNumber = 1;
      this.saleVolumeList = [];

      this.getMySalesDetail();
      // this.getSaleVolumeItem();
    },
    // 跳转到销售额
    goToSalesAmount(item) {
      this.$router.push({
        path: "/sales_amount",
        query: {
          goodsId: item.goodsId,
          startDate: this.date,
          goodsName: item.goodsName,
          size: item.size,
          saleMoney: item.saleMoney,
          saleDiscount: item.saleDiscount
        }
      });
    }
  }
};
</script>

<style scoped>
.my-sales {
  height: 100%;
}

.section-1,
.section-2 {
  /* background-color: #fff; */
  text-align: center;
}

.section-2 {
  max-height: calc(100% - 196px);
}

.products-item {
  height: 90%;
  padding-left: 10px;
  display: flex;
  flex-direction: column;
}

.item {
  margin-bottom: 5px;
}

.products-item p {
  text-align: left;
  display: flex;
  font-size: 0.85rem;
  /* justify-content: space-between; */
}

.products-item p:first-child {
  display: flex;
  flex-direction: column;
  font-size: 1rem;
  /* font-weight: bold; */
  flex: 1;
  color: #716e72;
}

.price {
  color: var(--money-up);
}

.size {
  font-size: 0.85rem;
}
</style>